package com.cardone.common.util.support;

import com.cardone.context.ContextHolder;
import com.cardone.context.function.Execution4Function;
import com.google.common.collect.Maps;
import lombok.Setter;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.beans.factory.InitializingBean;

import java.beans.PropertyEditor;
import java.util.Map;

/**
 * Created by Administrator on 2014/11/14.
 */
public class ReadRowFunctionDefaultSupport implements Execution4Function<Map<String, Object>, Row, Integer, Map<Integer, String>, Map<String, Object>>, InitializingBean {
    @Setter
    private Map<String, String> typeMap;
    private Map<String, PropertyEditor> propertyEditorMap;
    /**
     * 是否允许空行
     */
    @Setter
    private boolean allowEmptyRow = false;

    @Override
    public Map<String, Object> execution(Row row, Integer integer, Map<Integer, String> columns, Map<String, Object> paramMap) {
        final Map<String, Object> map = Maps.newHashMap();

        int notEmptyCells = 0;

        for (final Map.Entry<Integer, String> columnEntry : columns.entrySet()) {
            String key = columnEntry.getValue();

            if (key == null) {
                continue;
            }

            final Cell cell = row.getCell(columnEntry.getKey());

            if (cell == null) {
                map.put(key, null);

                continue;
            }

            final int type = cell.getCellType();

            if (Cell.CELL_TYPE_STRING != type) {
                cell.setCellType(Cell.CELL_TYPE_STRING);
            }

            String value = cell.getStringCellValue();

            if (value == null) {
                map.put(key, null);

                continue;
            }

            if (StringUtils.isNotBlank(value)) {
                notEmptyCells++;
            }

            if (MapUtils.isEmpty(propertyEditorMap) || !propertyEditorMap.containsKey(key)) {
                map.put(key, value);

                continue;
            }

            PropertyEditor propertyEditor = propertyEditorMap.get(key);

            propertyEditor.setAsText(value);

            map.put(key, propertyEditor.getValue());
        }

        if (!allowEmptyRow && (notEmptyCells < 1)) {
            return null;
        }

        return map;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        if (MapUtils.isEmpty(typeMap)) {
            return;
        }

        propertyEditorMap = Maps.newHashMap();

        for (Map.Entry<String, String> typeEntry : typeMap.entrySet()) {
            if (StringUtils.isBlank(typeEntry.getValue())) {
                continue;
            }

            String[] columnNames = StringUtils.split(typeEntry.getValue(), ",");

            if (ArrayUtils.isEmpty(columnNames)) {
                continue;
            }

            PropertyEditor propertyEditor = ContextHolder.getBean(PropertyEditor.class, typeEntry.getKey());

            if (propertyEditor == null) {
                continue;
            }

            for (String columnName : columnNames) {
                propertyEditorMap.put(columnName, propertyEditor);
            }
        }
    }
}
